<?php
class Event_Model extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }

    public function check_warting() {
    	$uid = $this->user->check_need_user();
    	if ($uid == NULL)
    		return;

    	$posts = $this->user->get_all_posts($uid);

    	$count = array();
    	foreach($posts AS $post) {
    		$count[md5($post->content)][] = $post;
    	}

    	$event = FALSE;
    	foreach($count AS $value) {
    		if (count($value) >= 3) {
    			//3条以上当做批量刷回复
    			$event = TRUE;
    			$this->db->insert('event', array(
    					'uid'	=> $uid,
    					'type'	=> $this->user->TYPE_WATERING,
    					'result'	=> $this->user->RESULT_PADDING,
    					'addition'	=> serialize($value)
    				));
    			$this->common->log('info', "user: {$uid} has been discoverd an warting event");
    		}
    	}

    	if (!$event) {
    		//没有插入数据，插入一个pass表示这个用户通过了
    		$this->common->log('info', "user: {$uid} has passed warting test");
    		$this->db->insert('event', array(
    				'uid'	=> $uid,
    				'type'	=> $this->user->TYPE_WATERING,
    				'result'	=> $this->user->RESULT_PASS
    			));
    	}
    }

    public function all_events() {
    	$sql = 'SELECT event.*, user.name AS username FROM event 
    				LEFT JOIN user ON event.uid = user.id
    			ORDER BY time, uid';
    	return $this->db->query($sql)->result();
    }

    public function add_black_list($username = '') {
        if ($username == '')
            return FALSE;
        $data = $this->common->fetch_with_post("http://tieba.baidu.com/f/like/commit/black/add", 
                                    array(
                                            'tbs'   => $this->user->get_tbs(),
                                            'usn'   => $username,
                                            'ie'    => "utf-8",
                                            'fid'   => "26074",
                                            'fname' => "浙江大学"
                                        ),
                                    FALSE);
        return json_decode($data);
    }

    public function save_operation($array = array()) {
        $undo = array();
        foreach($array AS $key => $value) {
            if ($this->db->where('url', $value['url'])->get('operation')->num_rows() == 0) {
                //没有插入这一条
                $undo[]  = $value;
                $this->db->insert('operation', $value);
            } else {
                //插入过了，那就重复了，不管了。。。
            }
        }
        return $undo;
    }

    public function post_reply($content, $url) {
        $post = array();
        $post['co'] = $content;
        $post['_t']                 = round(microtime(true) * 1000);
        $post['tag']                = 11;
        $post['upload_img_info']    = '';
        $post['src']                = 1;
        $post['fid']                = 26074;
        $post['word']               = '浙江大学';
        $post['tbs']                = $this->user->get_tbs();
        $post['z']                  = '2859677878';
        $post['lp']                 = '6026';

        $data = $this->common->fetch_with_post('http://tieba.baidu.com/mo/q/apubpost?_t=' . round(microtime(true) * 1000), $post, FALSE, "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19");
        var_dump($post);
        var_dump($data);

        $data = json_decode($data);

        if ($data != 0) {
            //没有发送成功，删掉好了
            $this->db->where('url', $url)->delete('operation');
        }
    }
}
?>